package com.aerospace.serviceprovider.socket;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketReceiveServer {
    public static final int PORT = 9999;
    public static void main(String[] args) {
        try {

//            ListenThread listenThread = new ListenThread();
//            Thread a = new Thread(listenThread);
//            a.start();

            start();
//            start2();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void start() throws IOException{
        ServerSocket serverSocket = new ServerSocket(PORT);
        Socket s = serverSocket.accept();
        SessionUtils.socketMap.put("client",s);

        System.out.println(SessionUtils.socketMap);
        System.out.println("循环监听socket开始");
        System.out.println("建立socket链接");
        String serverMessage = "收到客户端呼叫！！！";
        DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
        System.out.println("客户端呼叫显示===》》》" + dis.readUTF());
        DataOutputStream ps = new DataOutputStream(s.getOutputStream());
        ps.writeUTF(serverMessage);
        ps.flush();
        DataOutputStream ps2 = new DataOutputStream(s.getOutputStream());
        ps2.writeUTF(serverMessage);
        ps2.flush();
        while (true){
//            dis.close();
//            ps.close();
        }
    }


    public static void start2(){
        Socket s = null;
        System.out.println("start2kaishi");
        try{
            while(s == null){
                s = SessionUtils.socketMap.get("client");
//                System.out.println(s);
            }
            System.out.println(s);
//            System.out.println(s.getInetAddress().getHostAddress());
//            String serverMessage = "服务端向客户端发送消息";
//            DataOutputStream ps = new DataOutputStream(s.getOutputStream());
//            ps.writeUTF(serverMessage);
//            ps.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}


class ListenThread implements Runnable{

    @Override
    public void run() {
        try{
            ServerSocket serverSocket = new ServerSocket(9999);
            System.out.println("kaishi线程");
            Socket s = serverSocket.accept();
            SessionUtils.socketMap.put("client",s);

            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println(SessionUtils.socketMap);
            System.out.println("循环监听socket开始");
            System.out.println("建立socket链接");
            String serverMessage = "收到客户端呼叫！！！";
            DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
            System.out.println("客户端呼叫显示===》》》" + dis.readUTF());
            DataOutputStream ps = new DataOutputStream(s.getOutputStream());
            ps.writeUTF(serverMessage);
            ps.flush();


            while (true){
//            dis.close();
//            ps.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}